package com.tjetc.service.impl;

import com.tjetc.common.JsonResult;
import com.tjetc.dao.TravelogueMapper;
import com.tjetc.entity.Travelogue;
import com.tjetc.service.TravelogueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TravelogueServiceImpl implements TravelogueService {

    @Autowired
    private TravelogueMapper travelogueMapper;

    @Override
    public JsonResult<List<Travelogue>> findAll(Long is_editor) {
        List<Travelogue> list = travelogueMapper.selectAll(is_editor);
        return JsonResult.success(list);
    }

    @Override
    public JsonResult<Travelogue> findById(Long travelogueId) {
        Travelogue travelogue = travelogueMapper.selectById(travelogueId);
        return travelogue != null ? JsonResult.success(travelogue) : JsonResult.fail("游记不存在");
    }

    @Override
    @Transactional
    public JsonResult<Void> addTravelogue(Travelogue travelogue) {
        int result = travelogueMapper.addNewTravelogue(travelogue);
        return result > 0 ? JsonResult.success("新增成功") : JsonResult.fail("新增失败");
    }
    @Override
    @Transactional
    public JsonResult<Void> createNewTravelogue(Travelogue travelogue) {
        // 设置默认值
        if (travelogue.getCreateTime() == null) {
            travelogue.setCreateTime(LocalDateTime.now());
        }
        if (travelogue.getUpdateTime() == null) {
            travelogue.setUpdateTime(LocalDateTime.now());
        }
        if (travelogue.getViewCount() == null) {
            travelogue.setViewCount(0L);
        }
        if (travelogue.getLikeCount() == null) {
            travelogue.setLikeCount(0L);
        }
        if (travelogue.getCommentCount() == null) {
            travelogue.setCommentCount(0L);
        }
        if (travelogue.getIsFeatured() == null) {
            travelogue.setIsFeatured(false);
        }
        if (travelogue.getIsEditor() == null) {
            travelogue.setIsEditor(false);
        }
        if (travelogue.getStatus() == null) {
            travelogue.setStatus(1); // 默认为已发布状态
        }

        // 执行插入操作
        int result = travelogueMapper.createTravelogueRecord(travelogue);

        // 返回结果
        return result > 0 ? JsonResult.success("新增成功") : JsonResult.fail("新增失败");
    }

    @Override
    @Transactional
    public boolean addNewTravelogue(Travelogue travelogue) {
        // Set default values if needed
        if (travelogue.getCreateTime() == null) {
            travelogue.setCreateTime(LocalDateTime.now());
        }
        if (travelogue.getUpdateTime() == null) {
            travelogue.setUpdateTime(travelogue.getCreateTime());
        }


        return travelogueMapper.addNewTravelogue(travelogue) > 0;
    }

    @Override
    @Transactional
    public JsonResult<Void> updateTravelogue(Travelogue travelogue) {
        int result = travelogueMapper.update(travelogue);
        return result > 0 ? JsonResult.success("更新成功") : JsonResult.fail("更新失败");
    }

    @Override
    @Transactional
    public JsonResult<Void> deleteById(Long travelogueId) {
        int result = travelogueMapper.deleteById(travelogueId);
        return result > 0 ? JsonResult.success("删除成功") : JsonResult.fail("删除失败");
    }

    @Override
    public JsonResult getTravelogues(Integer l) {
        List<Travelogue> travelogues=travelogueMapper.getTop(l);
        return JsonResult.success("成功查询到热门游记",travelogues);
    }

    @Override
    public JsonResult findByCitySpots(Map<String, Object> params) {
        // 获取参数
        List<Long> spotIds = (List<Long>) params.get("spotIds");
        Boolean isFeatured = (Boolean) params.get("isFeatured");
        Boolean isEditor = (Boolean) params.get("isEditor");
        String sortBy = (String) params.get("sortBy");
        Boolean includeUserInfo = (Boolean) params.getOrDefault("includeUserInfo", false);

        // 验证景点ID列表是否为空
        if (spotIds == null || spotIds.isEmpty()) {
            return JsonResult.fail("未找到该城市的景点信息");
        }

        // 查询城市下所有景点的游记
        List<Travelogue> travelogues = travelogueMapper.findByCitySpots(spotIds, isFeatured, isEditor, sortBy);
        
        // 构建响应数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", travelogues.size());
        resultMap.put("travelogues", travelogues);
        
        return JsonResult.success("查询成功", resultMap);
    }

    @Override
    public JsonResult<com.tjetc.common.vo.user.TravelogueView> findArticleWithAuthorById(Long travelogueId) {
        com.tjetc.common.vo.user.TravelogueView travelogue = travelogueMapper.selectAllArticalById(travelogueId);
        return travelogue != null ? JsonResult.success(travelogue) : JsonResult.fail("游记不存在");
    }
}
